package com.example.yls.demoa;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private ImageView iv_wifi;
    private ImageView iv_starpenguins;
    private Button btn_star;
    private Button btn_stop;
    private Button btn_starpenguins;
    private AnimationDrawable animationDrawable;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv_wifi = findViewById(R.id.iv_wifi);
        iv_starpenguins = findViewById(R.id.iv_penguins);
        btn_star = findViewById(R.id.btn_star);
        btn_stop = findViewById(R.id.btn_stop);
        btn_starpenguins = findViewById(R.id.btn_starpenguins);

        animationDrawable = (AnimationDrawable) iv_wifi.getDrawable();
        btn_star.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                animationDrawable.start();

            }
        });

        btn_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                animationDrawable.stop();

            }
        });

        btn_starpenguins.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //透明度
//                starAlphaAnim();
                //平移
//                starTransAnim();
                //缩放
//                starScaleAnim();
                //旋转
//                starRotateAnim();

                //集合
                starSetAnim();

            }
        });

    }

    private void starSetAnim() {
        //第一种 使用代码
//        AnimationSet animationSet = new AnimationSet(true);
//
//        RotateAnimation rotateAnimation = new RotateAnimation(0,
//              360,50,100);
//
//        ScaleAnimation scaleAnimation = new ScaleAnimation(1, 3, 1, 3, 100, 100);
//
//        TranslateAnimation translateAnimation = new TranslateAnimation(0, 0,
//                0, -300);
//        animationSet.addAnimation(rotateAnimation);
//        animationSet.addAnimation(scaleAnimation);
//        animationSet.addAnimation(translateAnimation);
//        animationSet.setDuration(5000);
//        animationSet.setRepeatMode(Animation.REVERSE);
//        animationSet.setRepeatCount(4);
//        iv_starpenguins.startAnimation(animationSet);

        //第二种 使用xml
        AnimationSet animationSet = (AnimationSet) AnimationUtils.loadAnimation(MainActivity.this
        ,R.anim.alpah_set);
        iv_starpenguins.startAnimation(animationSet);

    }

    //旋转
    private void starRotateAnim() {
        //第一种 使用代码
//        RotateAnimation rotateAnimation = new RotateAnimation(100,
//                360,50,100);
//        rotateAnimation.setDuration(3000);
//        rotateAnimation.setRepeatCount(3);
//        rotateAnimation.setRepeatMode(Animation.REVERSE);
//        iv_starpenguins.startAnimation(rotateAnimation);

        //第二种 使用xml
        RotateAnimation rotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation(MainActivity.this
        ,R.anim.alpah_rotate);
        iv_starpenguins.startAnimation(rotateAnimation);
    }

    //缩放
    private void starScaleAnim() {

        //第一种 使用代码
//        ScaleAnimation scaleAnimation = new ScaleAnimation(1, 3, 1, 3, 100, 100);
//        scaleAnimation.setFillAfter(true);
//        scaleAnimation.setDuration(3000);
//        scaleAnimation.setRepeatMode(Animation.REVERSE);
//        scaleAnimation.setRepeatCount(Animation.INFINITE);
//        iv_starpenguins.startAnimation(scaleAnimation);

        //第二种 使用xml
        ScaleAnimation scaleAnimation = (ScaleAnimation) AnimationUtils.loadAnimation(MainActivity.this,
                R.anim.alpah_scale);
        iv_starpenguins.startAnimation(scaleAnimation);


    }

    //平移
    private void starTransAnim() {
        //第一种 使用代码
//        TranslateAnimation translateAnimation = new TranslateAnimation(0, 300,
//                0, 300);
//        translateAnimation.setFillAfter(true);
//        translateAnimation.setDuration(3000);
//        translateAnimation.setRepeatMode(Animation.REVERSE);
//        translateAnimation.setRepeatCount(Animation.INFINITE);
//        iv_starpenguins.startAnimation(translateAnimation);

        //第二种 使用xml

        TranslateAnimation translateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(MainActivity.this,
                R.anim.alpah_translate);
        iv_starpenguins.startAnimation(translateAnimation);

    }

    //透明度
    private void starAlphaAnim() {
        //第一种 使用XML
//        AlphaAnimation alphaAnimation = (AlphaAnimation) AnimationUtils.loadAnimation(MainActivity.this,
//                R.anim.alpah_anim);
//        iv_starpenguins.startAnimation(alphaAnimation);

        //第一种 使用代码
        AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setDuration(3000);
        alphaAnimation.setRepeatMode(Animation.REVERSE);
        alphaAnimation.setRepeatCount(Animation.INFINITE);
        iv_starpenguins.startAnimation(alphaAnimation);
    }
}
